﻿$(document).ready(function(){
  //$('#login-process').hide();
  
	}
);

$(function(){
    //$('#dlg-login').modal('show');
    
    $('#login-btn').click(function(){
      //alert('test');
      var username = $('#input-username').val();
      var password = $('#input-password').val();
      Client.login(username,password);
      $('#panel-login').hide();
      //$('#panel-chat').show();
    });
    $('#register-btn').click(function(){
      var username = $('#input-username').val();
      var password = $('#input-password').val();
      Client.register(username,password);
      $('#panel-login').hide();
    });
    $('#test-btn').click(function(){
      $('#panel-login').toggle();
    });
    var $grid = null ;
    function InitStatusTable(array){
     var data = [];
     array.forEach(function(record){
        var row = [];
        row[0] = record.DevName ;
        row[1] = record.DevNo;
        row[2] = record.MonitorContent;
        row[3] = record.MonitorValue;
        row[4] = record.MonitorDate;
        row[5] = record.CreateDate ;
        data.push(row);
        //$grid.pqGrid("refreshDataAndView");
      //$("#grid_array").append("<tr><td>"+record.DevName+"</td><td>"+record.DevNo+"</td></tr>");
      });        
    var obj = {};
    obj.width = 1024;
    obj.height = 768;
    obj.colModel = [{title:"DevName", width:70, dataType:"integer"},
        {title:"DevNo", width:50, dataType:"integer"},
        {title:"MonitorContent", width:150, dataType:"string", align:"right"},
        {title:"MonitorValue", width:150, dataType:"string", align:"right"},
        {title:"MonitorDate", width:150, dataType:"date", align:"right"},
        {title:"CreateDate", width:150, dataType:"date", align:"right"}];
    obj.dataModel = {data:data};
    $grid=$("#grid_array").pqGrid( obj );  
    }
    function UpdateStatus(jsonStr){
   // alert('jsonStr');
      var array = JSON.parse(jsonStr);
      if($grid==null){
        InitStatusTable(array);
      }else{
        var DM = $grid.pqGrid("option", "dataModel");
        var data = DM.data;
        var row = [];
      
      //alert(array[0].DevName);
      array.forEach(function(record){
        var rowIndx = 0;
        data.forEach(function(subData){
          rowIndx++;
          if(subData[0] == record.DevName){
              subData[0] = record.DevName ;
              subData[1] = record.DevNo;
              subData[2] = record.MonitorContent;
              subData[3] = record.MonitorValue;
              subData[4] = record.MonitorDate;
              subData[5] = record.CreateDate ;          
            $grid.pqGrid("refreshRow", { rowIndx: rowIndx }).pqGrid('setSelection', { rowIndx: rowIndx });

          }
        });

        //data.push(row);
        //$grid.pqGrid("refreshDataAndView");
      });
      }

    }
    Client.init(UpdateStatus);
    //$("#grid_array").pqGrid(obj);
    //alert(window.location.host);
    /*
    $('#form-login').on('click','button',function(event){
      var username = $('#inputUsername').val();
      var password = $('#inputPassword').val();
      //alert(username+';'+password);
      Client.login(username,password);
      //$('#login-panel').addClass('hideClass');
      //$('.hide').hide().removeClass('hide');
      //$('#form-register').css('display', 'none');
      //
      //$('#login-process').show();
      //$('#dlg-login').modal('hide');
      //event.stopPropagation(); 
      //$('#dlg-login-process').modal('show');
      //$('#login-process').removeClass('hideClass');
      
    });
    $('#form-register').on('click','button',function(){
      var username = $('#inputUsername').val();
      var password = $('#inputPassword').val();
      //alert(username+';'+password);
      Client.register(username,password);
    });
    $('#nickdetails').on('click','button',function(){
      //$('#dlg-login').modal('show');
      $('#conversation-panel').toggle();
    });*/
 
    
});